import Taro from '@tarojs/taro'

const TOKEN = '949ecf55aa01e34738a8c2d3c1e46e441ed29b696fc8066535a818c85574ea6b92b2f6f79ea026baffa2015168b55025a28263e27a3dcbd5a2a4277d9b2f85b46fd5651176c44f003cabea8098f32d4575e503a86d32f8ab84a2e127dcf7e464894476bb62a62ae16985eb0affeb0e9a269bf9aa5b3f5dfd0478a51f27b953b6'

const request = (url, data = {}, method = 'GET') => {
  return new Promise((resolve, reject) => {
    Taro.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/json',
        'Authorization': `bearer ${TOKEN}`
      },
      success: function(res) {
        if (res.statusCode == 200) {
          resolve(res.data)
        } else {
          reject(res.error)
        }
      },
      fail: function(err) {
        console.error('strapi error:', err)
        reject(err)
      }
    })
  });
}

export const strapi = {
  get: (url, data) => request(url, data, 'GET'),
  post: (url, data) => request(url, data, 'POST'),
  put: (url, data) => request(url, data, 'PUT'),
}
